// 338.比特位计数
package Leetcode_Second_Brush;

public class Solution338 {
    public int[] countBits(int n) {
        int[] result = new int[n + 1];

        for (int i = 0; i < n + 1; i++) {
            int num = i;
            int count = 0;
            while (num != 0) {
                num = num & (num - 1);
                count++;
            }
            result[i] = count;
        }
        
        return result;
    }
}
